#! /bin/sh

PATH=..:$PATH

echo "Testing PostgreSQL compilation..."

LC_CTYPE=gr_GR.ISO8859-7
LC_COLLATE=$LC_CTYPE
export LC_CTYPE LC_COLLATE

echo "Testing LC_CTYPE..."
if ! test-ctype > gr-ctype.out; then
   exit 1
fi
diff expected/gr-ctype.out gr-ctype.out

echo "Testing LC_COLLATE..."
perl ../sort-test.pl test-gr-sort.in > test-gr-sort.out
diff expected/test-gr-sort.out test-gr-sort.out

### If you have Python - uncomment the following two lines
#python ../sort-test.py test-gr-sort.in > test-gr-sort.out
#diff expected/test-gr-sort.out test-gr-sort.out


abort() {
   [ "$1" ] && echo "$*"
   exit 1
}

for f in char varchar text; do
   if echo $f | grep -q char; then
      ftype="$f(60)"
   else
      ftype="$f"
   fi
   echo "Testing PgSQL: sort on $ftype type..."

   dropdb testlocale >/dev/null 2>&1
   createdb testlocale || abort "createdb failed"
   psql -X -d testlocale -c "CREATE TABLE usastates (abbrev char(2), name_en char(20), name_gr $ftype);"  >/dev/null 2>&1 || abort "createtable failed"
   psql -X testlocale < test-gr.sql.in > test-gr-$f.sql.out 2>/dev/null || abort "test query failed"
   diff expected/test-gr-$f.sql.out test-gr-$f.sql.out
done
echo "Testing PgSQL: select on regexp..."
psql -X testlocale < test-gr-select.sql.in > test-gr-select.sql.out 2>/dev/null || abort "select query failed"
diff expected/test-gr-select.sql.out test-gr-select.sql.out
dropdb testlocale || abort "dropdb failed"
echo "Finished."
